from pyqtgraph.flowchart import Node
import pyqtgraph as pg
import numpy as np
import pickle
import os

from pyqtgraph.parametertree import Parameter, ParameterTree

from PyQt5.QtWidgets import *
from ...functions.fithelper import mask_bound
class TestNode(Node):


	nodeName = 'testNode'
	nodePaths = [('Display',)]

	def __init__(self, name, **kwargs):
		terminals = {
			'image': {'io': 'in'},
			'mask': {'io': 'out'},
			'maskedImage': {'io': 'out'}
		}
		super().__init__(name, terminals=terminals, **kwargs)

		paras_property = [
			{'name': 'left', 'type': 'int', 'value': 100},
			{'name': 'right', 'type': 'int', 'value': 200},
			{'name': 'top', 'type': 'int', 'value': 100},
			{'name': 'bottom', 'type': 'int', 'value': 200}
		]

		self.paras = Parameter.create(name='params', type='group', children=paras_property)
		self.paratree = ParameterTree()
		self.paratree.setParameters(self.paras, showTop=False)


	def ctrlWidget(self):
		return self.paratree


	def process(self, image, display=True):
		newresult = {}

		mask=np.ma.ones((512,512))
		mask[self.paras['top']:self.paras['bottom'],self.paras['left']:self.paras['right']] = 0
		print('process test node')
		print(image)
		newresult['mask'] = mask
		#print(mask_bound(mask))
		newresult['maskedImage'] = np.ma.array(image, mask = mask)
		print("s2")
		print(mask_bound(newresult['maskedImage'].mask))
		#newresult['image'] = self.roi.getArrayRegion(image, imageItem)

		#newresult['maskedImage'] = np.ma.array(image, mask=newresult['mask'])

		return newresult

	# def setInput(self, **args):
	# 	"""Set the values on input terminals. For most nodes, this will happen automatically through Terminal.inputChanged.
	# 	This is normally only used for nodes with no connected inputs."""
	# 	changed = True
	# 	for k, v in args.items():
	# 		term = self._inputs[k]
	# 		# oldVal = term.value()
	# 		# if not eq(oldVal, v):
	# 		# 	changed = True
	# 		term.setValue(v, process=False)
	# 	if changed and '_updatesHandled_' not in args:
	# 		self.update()

	def close(self):
		super().close()


	def saveState(self):
		state = super().saveState()
		return state

	def restoreState(self, state):
		super().restoreState(state)


nodelist = [TestNode]